<?php

$error = 0;
$extension = "";

if (isset($_POST['submit'])) {

	$allowedExts = array("docx", "doc", "pdf");
	// Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document WORD
	$extension = end(explode(".", $_FILES["file"]["name"]));

	if ((($_FILES["file"]["type"] == "application/pdf") && ($_FILES["file"]["size"] < 20 * pow(10, 6)) && in_array($extension, $allowedExts))) {
		if ($_FILES["file"]["error"] > 0) {
			echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
		} else {

			$ip_name = $_POST['ip_name'];
			$ip_address = $_POST['ip_address'];
			$ip_mail = $_POST['ip_mail'];
			$ip_contact = $_POST['ip_contact'];
			$ip_idd = $_POST['ip_idd'];
			$ip_sex = $_POST['ip_sex'];
			$ip_ceden = $_POST['ip_ceden'];

			// echo $ip_name . "<br/>";
			// echo $ip_address . "<br/>";
			// echo $ip_mail . "<br/>";
			// echo $ip_contact . "<br/>";
			// echo $ip_idd . "<br/>";
			// echo $ip_sex . "<br/>";
			// echo $ip_ceden . "<br/>";

			$_FILES["file"]["name"] = $ip_name . "_" . $ip_contact . "_CV";
			
			if ($ip_ceden == "on") {
				$ip_ceden = "sim";
			} else {
				$ip_ceden = "não";
			}
			$text_file = "Nome: " . $ip_name . " - Morada:" . $ip_address . " - E-mail: " . $ip_mail . " - Contacto: " . $ip_contact . " - Idade: " . $ip_idd . " - Sexo: " . $ip_sex . " - Cedência de Dados: " . $ip_ceden . "\n";
			if ($file = fopen("upload/inscricoes.txt", "a+")) {
				fwrite($file, $text_file);
			}
			echo $text_file . "<br/>";

			echo $file;
			if (file_exists("upload/" . $_FILES["file"]["name"])) {
				echo $_FILES["file"]["name"] . " already exists. ";
			} else {

				move_uploaded_file($_FILES["file"]["tmp_name"], "upload/cvs/" . $_FILES["file"]["name"]);
				echo "Stored in: " . "upload/cvs/" . $_FILES["file"]["name"];
				// $output = shell_exec("ls");
				// echo "<br/>Output shell script: " . $output;
			}

		}
	} else {
		echo "Invalid file";
	}
} else {

}

header('Location: regist_sucess.php');
exit();
?>
